<template>
  <el-dialog
    v-if="dialogVisible"
    title="设计师等级"
    :visible.sync="dialogVisible"
    width="500px"
    :before-close="handleClose" >
    <el-form :model="designer" :rules="rules" ref="designer" label-width="100px" class="demo-ruleForm" v-loading="loading">
      <el-form-item label="等级名称" prop="name">
        <el-input v-model="designer.name" placeholder="请输入等级名称"></el-input>
      </el-form-item>
      <el-form-item label="等级" prop="grade">
        <el-input  v-model.number="designer.grade" placeholder="请输入等级"></el-input>
      </el-form-item>
      <el-form-item label="佣金比例(%)" prop="commissionRate">
        <!-- <el-input step-strictly  v-model="designer.commissionRate" placeholder="请输入佣金比例"></el-input> -->
        <el-input-number  :min="0" :max="100" step-strictly  v-model="designer.commissionRate" placeholder="请输入佣金比例"></el-input-number>
      </el-form-item>
      <el-form-item label="最小销售额" prop="minSalesAmount">
        <el-input-number  v-model.number="designer.minSalesAmount" placeholder="请输入最小销售额" :min="0" step-strictly></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="resetForm('designer')">取 消</el-button>
      <el-button type="primary" @click="submitForm('formValidate')" v-hasPermi="['admin:system:user:level:update','admin:system:user:level:save']">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { levelSaveApi, levelUpdateApi } from '@/api/designer'
  import {Debounce} from '@/utils/validate'
  const obj = {
    name:'',
    grade: 1,
    discount: '',
    icon: '',
    image: '',
    id: null
  }
  export default {
    name: "CreatGrade",
    props:{
      'designer':Object
    },
    data() {
      return {
        dialogVisible: false,
        formValidate: Object.assign({},obj),
        loading: false,
        rules:{
          name: [
            {  required: true, message: '请输入等级名称', trigger: 'blur' }
          ],
          grade: [
            {  required: true, message: '请输入等级', trigger: 'blur' },
            { type: 'number', message: '等级必须为数字值'}
          ],
          commissionRate: [
            {  required: true, message: '请输入佣金比例', trigger: 'blur'},
          ],
          minSalesAmount: [
            {  required: true, message: '请输入最小销售额', trigger: 'blur'},
            { type: 'number', message: '经验必须为数字值'}
          ]
        }
      }
    },
    methods:{
      handleClose() {
         this.$nextTick(() => {
          this.$refs.designer.resetFields();
        })
        this.dialogVisible = false;
      },
      submitForm:Debounce(function(formName) {
        this.$refs.designer.validate((valid) => {
          if (valid) {
            this.loading = true
            let data = {
              commissionRate:this.designer.commissionRate / 100,
              minSalesAmount:this.designer.minSalesAmount,
              grade:this.designer.grade,
              id:this.designer.id,
              name:this.designer.name
            };
            this.designer.id ? levelUpdateApi(data).then(res => {
              this.$message.success('编辑成功')
              this.loading = false
              this.handleClose()
              this.formValidate = Object.assign({},obj)
              this.$parent.getList()
            }).catch(() => {
              this.loading = false
            }): levelSaveApi({...this.designer, commissionRate: this.designer.commissionRate / 100}).then(res => {
              this.$message.success('添加成功')
              this.loading = false
              this.handleClose()
              this.formValidate = Object.assign({},obj)
              this.$parent.getList()
            }).catch(() => {
              this.loading = false
              this.formValidate = Object.assign({},obj)
            })
          } else {
            return false;
          }
        });
      }),
      resetForm(formName) {
        
        // this[formName] = {};
         this.$nextTick(() => {
          this.$refs.designer.resetFields();
        })
        this.dialogVisible = false;
      }
    }
  }
</script>

<style scoped>

</style>
